﻿using AutoMapper;
using MediatR;
using SmartProduction.Domain.采购委外订单;
using SmartProduction.ErrorCode;
using SmartProduction.Infarstructrue.Interface;
using SmartProduction.Read.API.Application.Commands.DepartCommand;
using SmartProduction.Read.API.Dto;

namespace SmartProduction.Read.API.Application.CommandHandlers.DepartCommandHandler
{
    /// <summary>
    /// 部门查询命令处理
    /// </summary>
    public class DepartQueryCommandHandler : IRequestHandler<DepartQueryCommand, APIResult<List<DepartDto>>>
    {
        private readonly IMapper mapper;

        /// <summary>
        /// 仓储-部门
        /// </summary>
        private readonly IBaseRepository<Depart> departRepository;
        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="mapper"></param>
        /// <param name="departRepository">仓储-部门</param>
        public DepartQueryCommandHandler(IMapper mapper, IBaseRepository<Depart> departRepository)
        {
            this.mapper = mapper;
            this.departRepository = departRepository;
        }
        /// <summary>
        /// 处理
        /// </summary>
        /// <param name="request">实体</param>
        /// <param name="cancellationToken">取消</param>
        /// <returns></returns>
        public Task<APIResult<List<DepartDto>>> Handle(DepartQueryCommand request, CancellationToken cancellationToken)
        {
            APIResult<List<DepartDto>> result = new APIResult<List<DepartDto>>();
            result.Code = APIEnums.查询成功;
            result.Msg = APIEnums.查询成功.ToString();

            List<DepartDto> dataList = new List<DepartDto>();
            //全部数据
            var allList = departRepository.GetAll().Result.ToList();
            //一级目录
            var list = departRepository.GetAll().Result.Where(c => c.ParentId == 0);
            foreach (var item in list)
            {
                DepartDto dto = new DepartDto();
                dto.Value = item.DepartId;
                dto.Label = item.DepartName;
                //dto.Children = new List<DepartmentDTO>();                              
                dataList.Add(dto);
                SetCascader(allList, dto);
            }

            result.data = dataList;

            return Task.FromResult(result);
        }


        /// <summary>
        /// 设置级联（递归调用）
        /// </summary>
        /// <param name="allList">所有数据</param>
        /// <param name="parent">上级目录</param>
        private void SetCascader(List<Depart> allList, DepartDto parent)
        {
            var list = allList.Where(c => c.ParentId == parent.Value);
            foreach (var item in list)
            {
                DepartDto dto = new DepartDto();
                dto.Value = item.DepartId;
                dto.Label = item.DepartName;
                //dto.Children = new List<DepartmentDTO>();
                //将本级目录添加到父级目录中
                parent.Children.Add(dto);
                //递归调用
                SetCascader(allList, dto);
            }
        }
    }
}
